#include "user_controller.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "user_service.h"
#include "xml.h"
#include "user.h"

static char *captcha = "4vbKM8jk0AZgh37PLsx5dfH5weCU2qaE2JV7Yui3GTrBy4toFp1zRDN1n06mOcS9W8XlI6Q";

void 
loginpage(struct http_request *request, struct http_respond *respond) 
{
    set_redirect(respond, "/pages/login.html");
}

void 
joinpage(struct http_request *request, struct http_respond *respond) 
{
    set_redirect(respond, "/pages/join.html");
}


void 
login(struct http_request *request, struct http_respond *respond) 
{
    /* 获取请求的参数 */
    char name[128]; 
    char password[64]; 
    memset(name, 0, sizeof(name));
    memset(password, 0, sizeof(password));
    if (get_parameter(request, "name", name, sizeof(name)) <= 0)
    { 
        snprintf(name, sizeof(name), "%s", "Unknow");
    }

    if (get_parameter(request, "password", password, sizeof(password)) <= 0)
    {
        snprintf(password, sizeof(password), "%s", "Unknow");
    }

    struct xml *root = NULL;
    if ((root = create_xml_root("root", NULL)) == NULL)
    {
        return ;
    }

    /* 调用service层 */
    struct xml *item = NULL;
    char respond_data[1024];
    if (service_login_check(name, password) == 0)
    {
        if ((item = create_xml_and_assigment("error_code", "0")) != NULL)
        {
            insert_xml_child(root, item);
        }
    }
    else
    {
        if ((item = create_xml_and_assigment("error_code", "-1")) != NULL)
        {
            insert_xml_child(root, item);
        }
    }
    print_xml(root, respond_data, sizeof(respond_data));
    respond_write(respond, respond_data, strlen(respond_data));
}

void 
get_captcha(struct http_request *request, struct http_respond *respond)
{
    char respond_data[16];
    if (get_parameter(request, "captcha", NULL, 0) == 0)
    {
        int length;
        length = strlen(captcha);
        printf("length = %d\n", length);
        int i;
        srand(time(0));
        for (i = 0; i < 6; i++)
        {
            snprintf(respond_data + i, sizeof(respond_data) - i, "%c", captcha[rand() % length]);
        }
        respond_write(respond, respond_data, strlen(respond_data));
        return ;
    }
    respond_write(respond, "Error", strlen("Error"));
}

void 
siginup(struct http_request *request, struct http_respond *respond) 
{
    /* 获取请求的参数 */
    char name[128]; 
    char password[64]; 
    char telephone[32];
    memset(name, 0, sizeof(name));
    memset(password, 0, sizeof(password));
    memset(telephone, 0, sizeof(telephone));

    /* respond xml */
    struct xml *root = NULL;
    if ((root = create_xml_root("root", NULL)) == NULL)
    {
        return ;
    }

    struct xml *item = NULL;
    if ((get_parameter(request, "name", name, sizeof(name)) <= 0)
     || (get_parameter(request, "password", password, sizeof(password)) <= 0)
     || (get_parameter(request, "telephone", telephone, sizeof(telephone)) <= 0)
     || (service_check_user_is_exists(name) == 0))
    { 
        if ((item = create_xml_and_assigment("error_code", "-1")) != NULL)
        {
            insert_xml_child(root, item);
        }
    }
    else
    {
        struct user u;
        u.age = 0;
        u.gender = 0;
        snprintf(u.name, sizeof(u.name), name);
        snprintf(u.password, sizeof(u.password), password);
        snprintf(u.telephone, sizeof(u.telephone), telephone);

        /* add user */
        if (service_add_user(&u) >= 0 && (item = create_xml_and_assigment("error_code", "0")) != NULL)
        {
            insert_xml_child(root, item);
        }
    }
    printf("name = %s, password = %s\n", name, password);

    /* 调用service层 */
    char respond_data[1024];
    print_xml(root, respond_data, sizeof(respond_data));
    respond_write(respond, respond_data, strlen(respond_data));
}

